package com.yty.newconsult.controller;

import com.yty.newconsult.common.Result;
import com.yty.newconsult.common.ResultGenerator;
import com.yty.newconsult.entity.DailyNews;
import com.yty.newconsult.entity.QueryMessage;
import com.yty.newconsult.service.DailyNewsService;
import com.yty.newconsult.service.InvokeApiService;
import com.yty.newconsult.service.QueryHistoryService;
import org.apache.catalina.util.ToStringUtil;
import org.apache.ibatis.annotations.Param;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.util.Date;
import java.util.List;

@RequestMapping("/consult")
@RestController
public class ConsultController {
    @Resource
    private InvokeApiService invokeApiService;
    @Resource
    private QueryHistoryService queryHistoryService;
    @Resource
    private DailyNewsService dailyNewsService;
    @RequestMapping("/queryContent")
    public Result queryContent(@RequestBody QueryMessage queryMessage){
        queryHistoryService.insert(queryMessage);
        String s=invokeApiService.queryContent(queryMessage.getQuery(),queryMessage.getType());
        return ResultGenerator.genSuccessResult(s);
    }
    @RequestMapping("/querySummary")
    public Result querySummary(@RequestBody String text, @RequestHeader("id") String id){
        System.out.println("Received text: " + text);  // 打印接收到的 text
        System.out.println("Received id: " + id);
        String response=invokeApiService.querySummary(id,text);
        return ResultGenerator.genSuccessResult(response);
    }
    @GetMapping("/getDailyNews")
    public ResponseEntity<?> getDailyNews(@RequestParam("date") @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate date) throws ParseException {
        return ResponseEntity.ok(dailyNewsService.queryByDate(date));
    }
    @GetMapping("/getLastestNews")
    public ResponseEntity<?> getLastestNews(@RequestParam("num") int num){
        return ResponseEntity.ok(dailyNewsService.queryLastestNews(num));
    }

}
